﻿using MyMusicPlay._4._0.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace MyMusicPlay._4._0
{
    /// <summary>
    /// 配置
    /// </summary>
    public class DispositionApp : BaseClass
    {
        public static DispositionApp dispositionApp;

        private Disposition disposition = new Disposition();
        public static Disposition Disposition { get => dispositionApp.disposition; }

        private string FilePath = Directory.GetCurrentDirectory();

        private string FileName = @"Disposition.json";

        private string path;

        public DispositionApp()
        {
            path = this.FilePath + '/' + this.FileName;
            if (!IsExist())
                CreateDisposition();
            dispositionApp = this;
        }

        public void Initialize()
        {
            dispositionApp.LoadDisposition();
        }

        private bool IsExist()
        {
            return File.Exists(path);
        }

        private void CreateDisposition()
        {
            File.Create(path).Close();
            WriteDispoition();
        }
        public void LoadDisposition()
        {
            using (StreamReader sr = new StreamReader(path))
            {
                var json = sr.ReadToEnd();
                dispositionApp.disposition = JsonConvert.DeserializeObject<Disposition>(json);
            }

            if (dispositionApp.disposition == null)
            {
                dispositionApp.disposition = new();
                return;
            }

            #region 加载歌单
            disposition.MusicList.ForEach(u =>
            {
                PlayApplication.GetModule<Media>().AddMusicListToAll(u.Name, u.Path);
            });
            #endregion
            #region 加载播放顺序
            var orderStateValue = disposition.OrderState;
            PlayApplication.GetModule<Media>().orderState = (Media.OrderState)orderStateValue;
            PlayApplication.GetMainWindow().CurOrderState.Text = ((Media.OrderState)orderStateValue).ToString();
            #endregion
            #region 加载背景图片
            var imageValue = disposition.BackImage;
            PlayApplication.GetMainWindow().UpdateBackImage(imageValue.Path);
            PlayApplication.GetMainWindow().UpdateBackEffect(imageValue.Effect);
            #endregion
            #region 加载背景色
            var rgb = disposition.Background.Color;
            PlayApplication.GetMainWindow().UpdateBackgroundColor(rgb);
            #endregion
            #region 加载透明度
            var opacity = disposition.Background.Opacity;
            PlayApplication.GetMainWindow().UpdateBackgroundOpacity(opacity);
            #endregion
            #region 加载播放框的背景图片
            var musicPlayingBoardImageValue = disposition.MusicPlayingBoardBackImage;
            PlayApplication.GetMainWindow().UpdateMusicPlayingBoardBackImage(musicPlayingBoardImageValue.Path);
            PlayApplication.GetMainWindow().UpdateMusicPlayingBoardBackEffect(musicPlayingBoardImageValue.Effect);
            PlayApplication.GetMainWindow().UpdateMusicPlayingBoardOpacity(musicPlayingBoardImageValue.Opacity);
            #endregion
        }
        public void WriteDispoition()
        {
            var json = JsonConvert.SerializeObject(disposition);
            using (StreamWriter sw = new(path))
            {
                sw.Write(json);
            }
        }




        #region Update
        public void UpdateDisposition(Action<Disposition> action)
        {
            action(disposition);
        }
        public void UpdateDispositionOfMusicList(int index, string name, string path)
        {
            MusicListJson cur = null;
            int i = 0;
            foreach (var item in dispositionApp.disposition.MusicList)
            {
                if (i == index)
                {
                    cur = item;
                }
                i++;
            }
            if (cur != null)
            {
                cur.Name = name;
                cur.Path = path;
            }
        }
        public void UpdateDispositionOfImage(string path)
        {
            dispositionApp.disposition.BackImage.Path = path;
        }
        public void UpdateDispositionOfBackEffect(double effect)
        {
            dispositionApp.disposition.BackImage.Effect = effect;
        }
        public void UpdateDispositionOfBackColor(int[] rgb)
        {
            ColorJson color = dispositionApp.disposition.Background.Color;
            if (color == null)
            {
                dispositionApp.disposition.Background.Color = new();
                color = dispositionApp.disposition.Background.Color;
            }
            color.R = Convert.ToByte(rgb[0]);
            color.G = Convert.ToByte(rgb[1]);
            color.B = Convert.ToByte(rgb[2]);
        }
        public void UpdateDispositionOfBackOpacity(double opacity)
        {
            dispositionApp.disposition.Background.Opacity = opacity;
        }
        public void UpdateDispositionOfOrderState(int index)
            => dispositionApp.disposition.OrderState = index;
        #endregion

        #region Delete
        public void DeleteDispositionOfMusicList(int index)
        {
            dispositionApp.disposition.MusicList.RemoveAt(index);
        }
        #endregion

        #region Read
        public List<MusicListJson> ReadDispositionOfMusicLists()
            => dispositionApp.disposition.MusicList ?? new();
        public BackImageJson ReadDispositionOfImage()
            => dispositionApp.disposition.BackImage ?? new();
        public ColorJson ReadDispositionOfBackgrounColor()
            => dispositionApp.disposition.Background.Color ?? new();
        public double ReadDispositionOfBackOpacity()
            => dispositionApp.disposition.Background.Opacity;
        public ColorJson ReadDispositionOfTextColor()
            => dispositionApp.disposition.TextColor ?? new();
        public ColorPlusJson ReadDispositionOfItemBackColor()
            => dispositionApp.disposition.ItemFocurBackground ?? new();
        public BackImageAndOpacityJson ReadDispositionOfMusicPlayingBoardBackColor()
            => dispositionApp.disposition.MusicPlayingBoardBackImage ?? new();
        #endregion

        #region Add
        public void AddDispositionOfMusicList(string name, string path)
        {
            dispositionApp.disposition.MusicList.Add(new MusicListJson() { Name = name, Path = path });
        }
        #endregion
    }
}
